"use strict";

const Controller = require("egg").Controller;
const jwt = require("jsonwebtoken");

class PcLoginController extends Controller {
    // 登录
    async login() {
        const { ctx, app } = this;
        const { username, password } = ctx.request.body;
        const data = await app.mysql.get("pclogin", {
            username: username,
            password: password,
        });
        if (data) {
            // 返回自定义网络响应码
            // ctx.response.status = 500;
            ctx.body = {
                code: "203",
                msg: "登录成功",
                token: jwt.sign(data.username, "sx"),
            };
        } else {
            ctx.body = {
                code: "000",
                msg: "用户名或密码错误",
            };
        }
    }

    async getUserInfo() {
        const { ctx, app } = this;
        const { token } = ctx.request.body;
        const newToken = jwt.verify(token, "sx");
        const data = await app.mysql.get("pclogin", { username: newToken });
        ctx.body = {
            code: "200",
            msg: "获取成功",
            data,
        };
    }

    async getAllUserInfo() {
        const { ctx, app } = this;
        const data = await app.mysql.select("pclogin");
        console.log(data);
        ctx.body = {
            code: "200",
            msg: "获取成功",
            data,
        };
    }
}

module.exports = PcLoginController;